package com.firebird.app.kotlin.stock.trend.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.firebird.app.kotlin.stock.trend.bean.Stock
import com.firebird.app.kotlin.stock.trend.R

/**
 * 市场行情页面中，各种排行榜的Listview空间的适配器
 * @author flan
 * @date   2015年10月28日
 */
class StockRankListViewAdapter(private val context: Context) : BaseAdapter() {
    private var stockList: List<Stock>? = null
    override fun getCount(): Int {
        return if (stockList != null) {
            stockList!!.size
        } else 0
    }

    override fun getItem(position: Int): Any? {
        return if (stockList != null) {
            stockList!![position]
        } else null
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    fun setDate(stockList: List<Stock>?) {
        this.stockList = stockList
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var mConvertView = convertView
        var holder: ViewHolder? = null
        if (mConvertView == null) {
            holder = ViewHolder()
            mConvertView = LayoutInflater.from(context).inflate(R.layout.item_lv_hs_phb, parent, false)
            holder.tv_name = mConvertView!!.findViewById<View>(R.id.stock_name) as TextView
            holder.tv_code = mConvertView!!.findViewById<View>(R.id.stock_code) as TextView
            holder.tv_price = mConvertView!!.findViewById<View>(R.id.stock_now_price) as TextView
            holder.tv_zdf = mConvertView!!.findViewById<View>(R.id.stock_zdf) as TextView
            mConvertView.tag = holder
        } else {
            holder = mConvertView.tag as ViewHolder
        }
        if (stockList != null && stockList!!.size > 0) {
            val stock = stockList!![position]
            holder.tv_name!!.text = stock.name
            holder.tv_code!!.text = stock.code
            holder.tv_price!!.text=stock.nowPrice.toString()
            holder.tv_zdf!!.text=stock.closePrice.toString() + "%"
        }
        return mConvertView
    }

    /**
     * 用于保存ListView中重用的item视图的引用
     */
    internal inner class ViewHolder {
        var tv_name: TextView? = null
        var tv_code: TextView? = null
        var tv_price: TextView? = null
        var tv_zdf: TextView? = null
    }

}